/**
 * Copyright &copy; 2019-2022 <a href="https://gitee.com/rit/zx-pm">zx-pm</a> All rights reserved.
 */
package com.zx.pm.modules.zx.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.zx.pm.common.config.Global;
import com.zx.pm.common.persistence.Page;
import com.zx.pm.common.utils.StringUtils;
import com.zx.pm.common.web.BaseController;
import com.zx.pm.modules.zx.entity.BidBond;
import com.zx.pm.modules.zx.entity.Lzd;
import com.zx.pm.modules.zx.entity.ProExperts;
import com.zx.pm.modules.zx.entity.ProSupplier;
import com.zx.pm.modules.zx.service.BidBondService;
import com.zx.pm.modules.zx.service.LzdService;
import com.zx.pm.modules.zx.service.TenderSectionService;

/**
 * 投标保证金Controller
 * @author forest
 * @version 2019-02-27
 */
@Controller
@RequestMapping(value = "${adminPath}/zx/bidBond")
public class BidBondController extends BaseController {

	@Autowired
	private LzdService lzdService;
	
	
	@Autowired
	private BidBondService bidBondService;
	
	@Autowired
	private TenderSectionService tenderSectionService;
	
	@ModelAttribute
	public BidBond get() {

		return new BidBond();
	}
	
	@RequiresPermissions("zx:bidBond:view")
	@RequestMapping(value = {"list", ""})
	public String list(BidBond bidBond, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<BidBond> page = bidBondService.findPage(new Page<BidBond>(request, response), bidBond); 
		model.addAttribute("page", page);
		return "modules/zx/bidBondList";
	}

	@RequiresPermissions("zx:bidBond:view")
	@RequestMapping(value = "form")
	public String form(BidBond bidBond, Model model) {
		ProSupplier ps = new ProSupplier();
		ps.setLzdId(bidBond.getLzdId());
		ps.setSectionId(bidBond.getSectionId());
		// 获取项目供应商信息
		List<ProSupplier> psList = bidBondService.findPayBidBondList(ps);
		// 获取项目信息
		bidBond.setLzd(lzdService.get(bidBond.getLzdId()));
		// 获取标段信息
		bidBond.setSection(tenderSectionService.get(bidBond.getSectionId()));
		bidBond.setDepositMoney(lzdService.getDepositMoney(bidBond.getLzdId(), bidBond.getSectionId()));
		model.addAttribute("bidBond", bidBond);
		model.addAttribute("psList", psList);
		return "modules/zx/bidBondForm";
	}
	
	@RequiresPermissions("zx:bidBond:view")
	@RequestMapping(value = "show")
	public String show(BidBond bidBond, Model model) {
		model.addAttribute("bidBond", bidBond);
		return "modules/zx/bidBondShow";
	}

	@RequiresPermissions("zx:bidBond:edit")
	@RequestMapping(value = "save")
	@ResponseBody
	public String save(BidBond bidBond, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, bidBond)){
			return form(bidBond, model);
		}
		bidBondService.save(bidBond);
		//addMessage(redirectAttributes, "保存投标保证金成功");
		//return "redirect:"+Global.getAdminPath()+"/zx/bidBond/?repage";
		return "0";
	}
	
	
	@RequiresPermissions("zx:bidBond:edit")
	@RequestMapping(value = "delete")
	public String delete(BidBond bidBond, RedirectAttributes redirectAttributes) {
		bidBondService.delete(bidBond);
		addMessage(redirectAttributes, "删除投标保证金成功");
		return "redirect:"+Global.getAdminPath()+"/zx/bidBond/?repage";
	}
	
	
	@RequestMapping(value = "updata")
	@ResponseBody
	public String updata(BidBond bidBond, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, bidBond)){
			return form(bidBond, model);
		}
		bidBondService.updata(bidBond);
		//addMessage(redirectAttributes, "数据更新成功");
		//return "redirect:"+Global.getAdminPath()+"/zx/bidBond/?repage";
		return "0";
	}
	
	@RequiresPermissions("zx:bidBond:edit")
	@RequestMapping(value = "payBidBond")
	public String payBidBond(HttpServletRequest request, Model model, BidBond bidBond) {
		// 获取投标保证金信息
		BidBond entity = bidBondService.get(bidBond);
		if(entity == null) {
			entity = new BidBond();
		}
		entity.setLzdId(bidBond.getLzdId());
		entity.setSectionId(bidBond.getSectionId());
		entity.setSupplierId(bidBond.getSupplierId());
		model.addAttribute("bidBond", entity);
		return "modules/zx/payBidBond";
	}
	
	
	@RequiresPermissions("zx:bidBond:view")
	@RequestMapping(value = "confirmPay")
	public String confirmPay(HttpServletRequest request, Model model, BidBond bidBond) {
		// 获取投标保证金信息
		BidBond entity = bidBondService.get(bidBond);
		if(entity == null) {
			entity = new BidBond();
		}
		entity.setLzdId(bidBond.getLzdId());
		entity.setSectionId(bidBond.getSectionId());
		entity.setSupplierId(bidBond.getSupplierId());
		model.addAttribute("bidBond", entity);
		return "modules/zx/confirmPay";
	}

	@RequestMapping(value = "batchConfirmPay")
	public String batchConfirmPay(HttpServletRequest request, RedirectAttributes redirectAttributes, BidBond bidBond) {
		if(bidBond != null && bidBond.getBbList().size() > 0) {
			for(BidBond b : bidBond.getBbList()) {
				if(StringUtils.isBlank(b.getReturnableResult()) && StringUtils.isBlank(b.getPayResult())) {
					break;
				}
				b.setLzdId(bidBond.getLzdId());
				b.setSectionId(bidBond.getSectionId());
				bidBondService.save(b);
			}
		}
		addMessage(redirectAttributes, "信息保存成功");
		return "redirect:"+Global.getAdminPath()+"/zx/bidBond/form/?lzdId="+ bidBond.getLzdId() +"&sectionId="+bidBond.getSectionId()+"&repage";
	}
}